<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑新闻</title>
    <link rel="stylesheet" type="text/css" href="$request.getContextPath()/resources/layui/css/layui.css">
    <style type="text/css">
        #newsLogo{
            width: 1080px;
            height: 400px;
            transition:all 0.3s;
            margin-left: 110px;
        }
        .show{
            display:block;
            visibility: visible;
        }
    </style>
</head>
<body>
<form class="layui-form" action="">
    <input type="hidden" name="newsId" value="$!news.id">
    <div class="layui-form-item">
        <label class="layui-form-label">新闻标题</label>
        <div class="layui-input-inline">
            <input type="text" name="title" value="$!news.title" required placeholder="请输入新闻标题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">新闻分类</label>
        <div class="layui-input-inline">
            <select name="categoryId" required lay-verify="required">
                #foreach($newsCategory in $newsCategorys)
                   #if($!news.categoryId==$!newsCategory.id)
                       <option value="$!newsCategory.id" selected="selected">$!newsCategory.categoryName</option>
                   #else
                       <option value="$!newsCategory.id">$!newsCategory.categoryName</option>
                   #end
                #end
            </select>
        </div>
        <div class="layui-form-mid layui-word-aux">指定新闻类别</div>
    </div>
    <div class="layui-form-item layui-form-text site-demo-upload">
        <label class="layui-form-label">新闻LOGO</label>
        <div class="site-demo-upbar">
            <input type="file" name="file" class="layui-upload-file">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <input type="hidden" name="logo" value="$!news.logo">
        <img id="newsLogo" src="$url.zimg()/$!news.logo">
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">新闻内容</label>
        <div class="layui-input-block">
            <textarea id="newsContent" name="content" placeholder="请输入新闻内容" class="layui-textarea">$!news.content</textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block submit-btn">
            <button class="layui-btn" lay-submit lay-filter="updateNewsForm">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
<script type="text/javascript" src="$request.getContextPath()/resources/jquery/jquery-3.1.0.min.js"></script>
<script type="text/javascript" src="$request.getContextPath()/resources/layui/layui.js"></script>
<script type="text/javascript" src="$request.getContextPath()/resources/layui/lay/modules/element.js"></script>
<script type="text/javascript">
    $(function(){
        layui.use(['form', 'layedit', 'laydate'], function(){
            var form = layui.form(),layer = layui.layer,layedit = layui.layedit,laydate = layui.laydate;
            layedit.set({
                uploadImage: {
                    url: '$request.getContextPath()/common/upload/layedit', //接口url
                    type: 'post' //默认post
                }
            });
            var newsContentIndex=layedit.build('newsContent',{height: 460}); //建立编辑器
            //监听提交
            form.on('submit(updateNewsForm)', function(data){
                var content=layedit.getContent(newsContentIndex);
                data.field.content=content;
                console.log(data.field);
                $.post("$request.getContextPath()/official/news/newsEdit",data.field,function(data){
                    //保存成功 关闭当前页
                    top.layer.msg(data.msg,{
                        icon: 1,
                        skin: 'demo-class',
                        title :'信息',
                        time:2000,
                        area: ['300px', '160px'],
                        offset: 'rb',
                        end :function(){
                            if(data.code==200){
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            }
                        }
                    });
                });
                return false;
            });
            //新闻logo
            layui.use('upload', function(){
                layui.upload({
                    url: '$request.getContextPath()/common/upload/img',
                    type:'images',
                    method: 'post',
                    success: function(res){
                        if(res.code==200){
                            $("#newsLogo").addClass("show");
                            $("#newsLogo").css("display", "block");
                            $("#newsLogo").attr("src","$!url.zimg()/"+res.data);
                            $("input[name='logo']").val(res.data);
                        }
                        layer.msg(res.msg,{
                            icon: 1,
                            skin: 'demo-class',
                            title :'信息',
                            area: ['300px', '160px'],
                            offset: 'rb'
                        });
                    }
                });
            });
        });
    });
</script>
</html>